package elitech.vietnam.weatherfashion;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import elitech.vietnam.weatherfashion.backgroundtask.NewPostTask;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.config.ImageHelper;
import elitech.vietnam.weatherfashion.dto.MemberDTO;

public class NewPostActivity extends Activity implements OnClickListener {

	public static final String CAM_PATH = Environment.getExternalStorageDirectory() + "/_camera.jpg";
	private EditText textbox_content;
	private ImageView image_post;
	private Button button_save;
	private String uri;
	private int request;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_post);

		Utils.Log(WFApplication.getInstance().todayData == null);
		textbox_content = (EditText) findViewById(R.id.newpost_textbox_content);
		image_post = (ImageView) findViewById(R.id.newpost_image);
		button_save = (Button) findViewById(R.id.newpost_button_save);

		this.uri = getIntent().getStringExtra("image");
		this.request = getIntent().getIntExtra("request", -1);

		if (this.uri == null || this.uri.equals(""))
			return;
		Utils.Log(uri);
		ProgressDialog dialog = new ProgressDialog(this);
		dialog.setTitle(getString(R.string.processing));
		dialog.setMessage(getString(R.string.please_wait_));
		dialog.show();
		if (ImageHelper.rotateImage(uri, CAM_PATH))
			uri = CAM_PATH;
		System.gc();
		ImageHelper.displayThumbImage(image_post, uri);
		dialog.dismiss();

		button_save.setOnClickListener(this);

	}

	@Override
	public void onBackPressed() {
		setResult(RESULT_CANCELED);
		finish();
	}

	@Override
	public void onClick(View arg0) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(textbox_content.getWindowToken(), 0);
		if (arg0.getId() == button_save.getId()) {
			MemberDTO user = GlobalValue.getInstance(getApplicationContext()).readUserInfo();
			String result = user.getId() + "_" + System.currentTimeMillis() + ".jpg";
			new NewPostTask(this).execute(uri, result, textbox_content.getText().toString().trim());
		}
	}
}
